package com.lzy.canal.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.lzy.canal.enums.MysqlTypeEnum;
import org.apache.commons.lang.time.DateUtils;

import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;

/**
 *
 */
public class CommonUtil {

    /**
     * 通过每个单元格的数据类型获取转换value
     *
     * @param column 列记录
     * @return
     */
    public static Object convertValueType(CanalEntry.Column column) {
        if (column.getIsNull()) {
            return null;
        }
        MysqlTypeEnum type = MysqlTypeEnum.getByName(column.getMysqlType());
        String value = column.getValue();
        if (MysqlTypeEnum.DECIMAL.equals(type)) {
            return new BigDecimal(value);
        }
        if (MysqlTypeEnum.DECIMAL_UNSIGNED.equals(type)) {
            return new BigDecimal(value);
        }
        if (MysqlTypeEnum.TINYINT.equals(type)) {
            return Integer.valueOf(value);
        }
        if (MysqlTypeEnum.TINYINT_UNSIGNED.equals(type)) {
            return Integer.valueOf(value);
        }
        if (MysqlTypeEnum.BOOLEAN.equals(type)) {
            return Boolean.valueOf(value);
        }
        if (MysqlTypeEnum.SMALLINT.equals(type)) {
            return Integer.valueOf(value);
        }
        if (MysqlTypeEnum.SMALLINT_UNSIGNED.equals(type)) {
            return Integer.valueOf(value);
        }
        if (MysqlTypeEnum.INT.equals(type)) {
            return Integer.valueOf(value);
        }
        if (MysqlTypeEnum.INT_UNSIGNED.equals(type)) {
            return Integer.valueOf(value);
        }
        if (MysqlTypeEnum.FLOAT.equals(type)) {
            return Float.valueOf(value);
        }
        if (MysqlTypeEnum.FLOAT_UNSIGNED.equals(type)) {
            return Float.valueOf(value);
        }
        if (MysqlTypeEnum.DOUBLE.equals(type)) {
            return Double.valueOf(value);
        }
        if (MysqlTypeEnum.DOUBLE_UNSIGNED.equals(type)) {
            return Double.valueOf(value);
        }
        if (MysqlTypeEnum.TIMESTAMP.equals(type)) {
            return Timestamp.valueOf(value);
        }
        if (MysqlTypeEnum.BIGINT.equals(type)) {
            return Long.valueOf(value);
        }
        if (MysqlTypeEnum.BIGINT_UNSIGNED.equals(type)) {
            return Long.valueOf(value);
        }
        if (MysqlTypeEnum.MEDIUMINT.equals(type)) {
            return Integer.valueOf(value);
        }
        if (MysqlTypeEnum.MEDIUMINT_UNSIGNED.equals(type)) {
            return Integer.valueOf(value);
        }
        if (MysqlTypeEnum.DATE.equals(type)) {
            try {
                return DateUtils.parseDate(value, new String[]{"yyyy-mm-dd"});
            } catch (ParseException e) {
                return value;
            }
        }
        if (MysqlTypeEnum.TIME.equals(type)) {
            return Time.valueOf(value);
        }
        if (MysqlTypeEnum.DATETIME.equals(type)) {
            return Timestamp.valueOf(value);
        }
        if (MysqlTypeEnum.VARCHAR.equals(type)) {
            return value;
        }
        if (MysqlTypeEnum.BIT.equals(type)) {
            return Integer.valueOf(value);
        }
        if (MysqlTypeEnum.JSON.equals(type)) {
            return JSON.parse(value);
        }
        if (MysqlTypeEnum.SET.equals(type)) {
            return String.valueOf(value);
        }
        if (MysqlTypeEnum.TINYTEXT.equals(type)) {
            return String.valueOf(value);
        }
        if (MysqlTypeEnum.MEDIUMTEXT.equals(type)) {
            return String.valueOf(value);
        }
        if (MysqlTypeEnum.LONGTEXT.equals(type)) {
            return String.valueOf(value);
        }
        if (MysqlTypeEnum.TEXT.equals(type)) {
            return String.valueOf(value);
        }
        if (MysqlTypeEnum.CHAR.equals(type)) {
            return String.valueOf(value);
        }
        return value;
    }
}
